<?php
namespace Home\Controller;
use Think\Controller;
/**
 * 婚礼
 */
class WeddingController extends BaseController
{
  // 商户列表
  public function MerchantList() {
    $page_index = I('page_index');
    $page_size = I('page_size')?:20;
    $token = I('token');
    if ($page_index < 1) {
      $data['code'] = '1001';
      $data['msg'] = '页码必须大于0';
      $this->error($data);
    }
    $m = M('merchant');
    $list = $m->page($page_index.','.$page_size)->field(array('id', 'uuid', 'nickname', 'avatar', 'banner', 'tagList'))->order('sort asc')->select();
    if (count($list) == 0) {
      $data['code'] = '999';
      if ($page_index > 1) {
          $data['msg'] = '没有更多了';
      }
      else {
          $data['msg'] = '数据为空';
      }
      $this->error($data);
    }
    $uid = $this->getMemberID($token);
    if ($uid != null) {
      $collect = M('collect');
      for ($i=0; $i < count($list); $i++) {
        $temp = $list[$i];
        $ll = $collect->where(array('uid' => $uid, 'collect_id' => $temp['id']))->find();
        if ($ll == null) {
          $temp['is_collect'] = 0;
        }
        else {
          $temp['is_collect'] = 1;
        }
        $list[$i] = $temp;
      }
    }

    $data['data'] = $list;
    $data['msg'] = '获取成功';
    $this->success($data);
  }
  // 商户详情
  public function MerchantDetail() {
    $mid = I('mid');
    $this->is_nullAction($mid);
    $m = M('merchant');
    // $Page = new Page($count, $listRows);// 实例化分页类 传入总记录数
    $list = $m->where(array('id' => $mid))->select();
    if (count($list) == 0) {
      $data['code'] = '999';
      $data['msg'] = '数据为空';
      $this->error($data);
    }
    $table_case = M('case');
    $temp = $list[0];
    $ee = $table_case->where(array('otherUuid' => $temp['uuid']))->select();
    $temp['myCase'] = $ee;
    $table_caseMedia = M('casemedia');
    for ($j=0; $j < count($ee); $j++) {
      $tmp = $temp['myCase'][$j]; // 里层字典tmp
      $ea = $table_caseMedia->where(array('uuid' => $tmp['uuid']))->select(); // 去关联图片-获取图片数组
      $ttt = null;
      for ($z=0; $z < count($ea); $z++) {
        $ttt[$z] = $ea[$z];
        $ttt[$z]['casustitle'] = $tmp['title'];
      }
      $temp['myCase'][$j]['imgs'] = $ttt;
    }
    $data['data'] = $temp;
    $data['msg'] = '获取成功';
    $this->success($data);
  }
  // 灵感
  public function InspirationList() {
    $page_index = I('page_index');
    $page_size = I('page_size')?:20;
    $token = I('token');
    if ($page_index < 1) {
      $data['code'] = '1001';
      $data['msg'] = '页码必须大于0';
      $this->error($data);
    }
    $m = M('inspiration');
    $count = $m->count(); //求出总数
    $list = $m->page($page_index.','.$page_size)->field(array('id', 'uuid', 'casustitle', 'img_url', 'caseuuid'))->order('id asc')->select();
    if (count($list) == 0) {
      $data['code'] = '999';
      if ($page_index > 1) {
          $data['msg'] = '没有更多了';
      }
      else {
          $data['msg'] = '数据为空';
      }
      $this->error($data);
    }
    $merchant = M('merchant');
    $array = null;
    $uid = $this->getMemberID($token);
    $collect = M('collect');
    for ($i=0; $i < count($list); $i++) {
      // 临时字典
      $temp = $list[$i];
      $merchantModel = $merchant->where(array('uuid' => $temp['caseuuid']))->find();
      $temp['telephone'] = $merchantModel['telephone']?:"";
      // 判断是否能获取到uid，获取到才知道点赞没有
      if ($uid != null) {
        // 收藏、点赞
        $ll = $collect->where(array('uid' => $uid, 'collect_id' => $temp['id']))->find();
        if ($ll != null) {
            $temp['is_collect'] = 1;
        }
        else {
            $temp['is_collect'] = 0;
        }
        $zan = M('zan');
        $ls = $zan->where(array('uid' => $uid, 'zan_id' => $temp['id']))->find();
        if ($ls != null) {
            $temp['is_zan'] = 1;
        }
        else {
          $temp['is_zan'] = 0;
        }
      }
      else {
        $temp['is_zan'] = 0;
        $temp['is_collect'] = 0;
      }
      $array[$i] = $temp;
    }
    $data['data'] = $array;
    $data['msg'] = '获取成功';
    $this->success($data);
  }

  public function InspirationDetail () {
    $uuid = I('uuid');
    $this->is_nullAction($uuid);

    $inspiration = M('inspiration');
    $merchant = M('merchant');
    $case = M('case');
    $caseMedia = M('casemedia');

    $inspirationModel = $inspiration->where(array('uuid' => $uuid))->find();
    // echo  $inspirationModel['caseuuid'];die;
    $merchantModel = $merchant->where(array('uuid' => $inspirationModel['caseuuid']))->find();

    $caseModel = $case->where(array('otherUuid' => $inspirationModel['caseuuid']))->find();

    $casemediaList = $caseMedia->where(array('uuid' => $caseModel['uuid']))->select();
    // 添加案例内容

    $caseModel['imgs'] = $casemediaList;
    $merchantModel['taglist'] = $inspirationModel['imgtaglist'];
    $merchantModel['myCase'][0] = $caseModel;
    $inspirationModel['merchant'] = $merchantModel;

    // for ($j=0; $j < count($ee); $j++) {
    //   $tmp = $temp['myCase'][$j]; // 里层字典tmp
    //   $ea = $table_caseMedia->where(array('uuid' => $tmp['uuid']))->select(); // 去关联图片-获取图片数组
    //   $temp['myCase'][$j]['imgs'] = $ea;
    // }
    $data['data'] = $inspirationModel;
    $data['msg'] = '获取成功';
    $this->success($data);
  }

  // 4个item前四个 + 案例
  public function ClassifyList() {
    $page_index = I('page_index');
    $page_size = I('page_size')?:20;
    $token = I('token');
    $param = I('item');
    $param = $param != '婚鞋'?$param:'跟妆';
    if ($page_index < 1) {
      $data['code'] = '999';
      $data['msg'] = '请输入正确页码';
      $this->error($data);
    }
    elseif ($param == null) {
      $data['code'] = '999';
      $data['msg'] = '没有传分类变量item';
      $this->error($data);
    }
    $m = M('wedding');
    $list = $m->where('type=\''.$param.'\' and status = 0')->order('sort asc')->page($page_index.','.$page_size)->select();
    if (count($list) == 0) {
      $data['code'] = '999';
      if ($page_index > 1) {
          $data['msg'] = '没有更多了';
      }
      else {
          $data['msg'] = '数据为空';
      }
      $this->error($data);
    }
    $collect = M('collect');
    $res = null;
    $data = null;
    $uid = $this->getMemberID($token);
    if ($uid) {
      for ($z=0; $z < count($list); $z++) {
        $ttmp = $list[$z];
        // echo $ttmp['id'];die;
        $collectModel = $collect->where(array('uid' => $uid,'collect_id' => $ttmp['id']))->find();
        if ($collectModel == null) {
          $ttmp['is_collect'] = 0;
        }
        else {
          $ttmp['is_collect'] = 1;
        }
        $res[$z] = $ttmp;
      }
      $data['data'] = $res;
    }
    else {
      $data['data'] = $list;
    }
    $data['msg'] = '获取成功';
    $this->success($data);
  }

  public function ClassifyDetail() {
    $param = I('itemId');
    $mm = M('wedding');
    $lists = $mm->where('id=\''.$param.'\'')->select();
    if (count($lists) == 0) {
      $mm = M('weddingtwo');
      $lists = $mm->where('id=\''.$param.'\'')->select();
      if (count($lists) == 0) {
        $data['code'] = '1001';
        $data['msg'] = 'itemId不存在';
        $this->error($data);
      }
    }
    $result = $lists[0];
    $m = M('media');
    $list = $m->where('meal_id=\''.$param.'\'')->select();
    $result['list'] = $list;
    // 计算高度
    $h = 20;
    $hei = 25;
    if ($result['wedding_time'] != null) {
      $h += $hei;
    }
    if ($result['wedding_location'] != null) {
      $h += $hei;
    }
    if ($result['wedding_theme'] != null) {
      $h += $hei;
    }
    if ($result['wedding_video'] != null) {
      $h += $hei;
    }
    if ($result['wedding_photo'] != null) {
      $h += $hei;
    }
    if ($result['wedding_makeup'] != null) {
      $h += $hei;
    }
    if ($result['wedding_emcee'] != null) {
      $h += $hei;
    }
    if ($result['wedding_floriculture'] != null) {
      $h += $hei;
    }
    if ($result['wedding_price'] != null) {
      $h += $hei;
    }
    $result['heightForCell'] = $h + 20;

    // 判断是否收藏
    $token = I('token');
    if ($token != null) {
      $uid = $this->getMemberID($token);
      if ($uid != null) {
        $cc = M('collect');
        $ll = $cc->where(array('uid' => $uid, 'collect_id' => $result['id']))->find();
        if ($ll != null) {
            $result['is_collect'] = 1;
        }
        else {
          $result['is_collect'] = 0;
        }
        $zan = M('zan');
        $ls = $zan->where(array('uid' => $uid, 'zan_id' => $result['id']))->find();
        if ($ls != null) {
            $result['is_zan'] = 1;
        }
        else {
          $result['is_zan'] = 0;
        }

      }
      else {
        $result['is_collect'] = 0;
        $result['is_zan'] = 0;
      }
    }
    else {
       $result['is_collect'] = 0;
       $result['is_zan'] = 0;
    }

    $data['data'] = $result;
    $data['msg'] = '获取成功';
    $this->success($data);
  }

  public function videoList() {
    $page_index = I('page_index');
    $page_size = I('page_size')?:20;
    $token = I('token');
    if ($page_index < 1) {
      $data['code'] = '999';
      $data['msg'] = '请输入正确页码';
      $this->error($data);
    }
    $m = M('video');
    $list = $m->page($page_index.','.$page_size)->select();
    if (count($list) == 0) {
      $data['code'] = '999';
      if ($page_index > 1) {
          $data['msg'] = '没有更多了';
      }
      else {
          $data['msg'] = '数据为空';
      }
      $this->error($data);
    }
    $uid = $this->getMemberID($token);
    if ($uid != null) {
      $collect = M('collect');
      for ($i=0; $i < count($list); $i++) {
        $temp = $list[$i];
        $ll = $collect->where(array('uid' => $uid, 'collect_id' => $temp['id']))->find();
        if ($ll == null) {
          $temp['is_collect'] = 0;
        }
        else {
          $temp['is_collect'] = 1;
        }
        $list[$i] = $temp;
      }
    }
    $data['data'] = $list;
    $data['msg'] = '获取成功';
    $this->success($data);
  }
  function playVideo() {
    $videoId = I('videoId');
    $this->is_nullAction($videoId);
    $m = M('video');
    $videoModel = $m->where(array('id' => $videoId))->find();
    if ($videoModel == null) {
      $data['code'] = '999';
      $data['msg'] = 'id不存在';
      $this->error($data);
    }
    // echo json_encode($videoModel);die;
    $videoModel['readarts'] = $videoModel['readarts'] + 1;
    // echo json_encode($videoModel);die;
    $m->save($videoModel);
    $data['msg'] = '提交成功';
    $this->success($data);
  }
}
?>
